/**
 * 近义词相关的模块
 */
define( [
  'jquery',
  '../../modules/createEditor',
  'constants',
  'api'
], function( $, ue, c, BIGWE_API ) {

  var $guideSimilar = $( '.guide_similar' );
  if ( $guideSimilar.length && sessionStorage.getItem( 'edit_text_guideSimilar' ) != 1 ) {

    ue.addListener( 'ready', function() {
      $( 'body' ).addClass( 'modal-open' ).css( {
        'padding-right': '17px'
      } );

      setTimeout( function() {
        var synonymsTool = $( '.edui-button.edui-for-synonyms' );
        var top = synonymsTool.offset().top;
        var left = synonymsTool.offset().left;

        $( '.js_img', $guideSimilar ).css( {
          left: left - 2,
          top: top - 2
        } );
        $( '.guide_container', $guideSimilar ).css( {
          left: left + 10,
          top: top - 32
        } );
        $guideSimilar.fadeIn( function() {
          sessionStorage.setItem( 'edit_text_guideSimilar', 1 );
        } );
      }, .5e3 );
    } );

    function closeGuide() {
      $( 'body' ).removeClass( 'modal-open' ).css( {
        'padding-right': ''
      } );

      $guideSimilar.hide();
    }

    $guideSimilar.on( 'click', '.js_close', closeGuide ).on( 'click', '.js_OK', function() {
      closeGuide();
      $( '.edui-for-synonyms>div>div>div' ).trigger( 'click' );
      var left = $( '.edui-dialog.edui-for-synonyms' ).css( 'left' );
      var top = $( '.edui-dialog.edui-for-synonyms' ).css( 'top' );
      $( '.edui-dialog-modalmask' ).css( 'opacity', '0' );
      $( '.use_similar_guide .use_similar_inner' ).css( {
        left: left,
        top: top
      } ).parent().fadeIn().one( 'click', '.text_inner3', function() {
        $( '.use_similar_guide' ).fadeOut();
        $( '.edui-dialog-modalmask' ).css( 'opacity', '' );
      } );
    } );
  }

  // 放到全局作用域以便
  window.similarWordRequest = function( findtxt, callback ) {

    return $.ajax( BIGWE_API.SIMILAR_WORDS.GET_SIMILAR_WORDS, {
      method: 'GET',
      timeout: 60e3,
      data: {
        word: findtxt
      },
      success: function( xhr ) {
        if ( $.isFunction( callback ) ) {
          callback.call( this, typeof xhr === 'string' ? JSON.parse( xhr ) : xhr );
        }
      }
    } );
  }
  ue.addListener( 'mousedown', function( e ) {
    this.mousedownTime = Date.now();
  } );
  // 在鼠标弹起事件中显示近义词划词
  ue.addListener( 'mouseup', function( e, a ) {
    // 关闭了划词 || 点击间隔时间小于150毫秒
    if ( window.localStorage.getItem( 'underlineSwicth' + c.userLogin ) == 0 || Date.now() - this.mousedownTime < 150 ) return;
    a = a || window.event;
    var top = a.clientX - parseInt( $( this.body ).css( 'padding-left' ) );
    var containerOffset = $( '.edui-editor-iframeholder', this.container ).offset();
    var containerTop = containerOffset.top + a.clientY + 20;
    var containerLeft = containerOffset.left + a.clientX - 10;
    var synonym = this.selection.getText() || '';
    var $synonym = $( '#synonym' );
    var $frame = $( '#synonym_frame', $synonym );
    if ( synonym !== '' && synonym.length <= 4 && /[\u4e00-\u9fa5]/.test( synonym ) ) {

      $frame[ 0 ].contentWindow.doquery( synonym );

      // 定位划词框的位置
      $synonym.css( {
        'top': containerTop,
        'left': containerLeft
      } ).show();

      $( document ).one( 'click', function() {
        $synonym.hide();
      } );
    }
  } );
} );